package com.lucaswangdev.logindemo.biz.service.impl;

import com.lucaswangdev.logindemo.biz.manager.ConceptManager;
import com.lucaswangdev.logindemo.biz.request.concept.*;
import com.lucaswangdev.logindemo.biz.response.common.PagingResponse;
import com.lucaswangdev.logindemo.biz.service.ConceptService;
import com.lucaswangdev.logindemo.constant.common.ErrorConstants;
import com.lucaswangdev.logindemo.exception.BusinessException;
import com.lucaswangdev.logindemo.model.convert.ConceptConverter;
import com.lucaswangdev.logindemo.model.domain.ConceptParam;
import com.lucaswangdev.logindemo.model.dto.ConceptDTO;
import com.lucaswangdev.logindemo.util.ValidatorUtils;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Lists;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Service
public class ConceptServiceImpl implements ConceptService {

    @Resource
    private ConceptManager conceptManager;

    @Override
    public Boolean create(ConceptCreateRequest conceptCreateRequest) {
        // 参数校验
        String errorMsg = ValidatorUtils.validateAndReturnSingleErrorMsg(conceptCreateRequest);
        if (StringUtils.isNotBlank(errorMsg)) {
            throw new BusinessException(ErrorConstants.PARAM_ERROR.getErrorCode(), errorMsg);
        }
        ConceptDTO conceptDTO = ConceptConverter.INSTANCE.convertRequest2DTO(conceptCreateRequest);
        conceptManager.create(conceptDTO);
        return Boolean.TRUE;
    }

    @Override
    public PagingResponse<ConceptDTO> listPage(ConceptQueryRequest queryRequest) {
        // 参数校验
        String errorMsg = ValidatorUtils.validateAndReturnSingleErrorMsg(queryRequest);
        if (StringUtils.isNotBlank(errorMsg)) {
            throw new BusinessException(ErrorConstants.PARAM_ERROR.getErrorCode(), errorMsg);
        }
        ConceptParam param = new ConceptParam();
        Integer currentPage;
        Integer pageSize;
        if (queryRequest.getCurrentPage() != null) {
            currentPage = queryRequest.getCurrentPage();
        } else {
            currentPage = 1;
        }
        if (queryRequest.getPageSize() != null) {
            pageSize = queryRequest.getPageSize();
        } else {
            pageSize = 20;
        }
        param.setCurrentPage(currentPage);
        param.setPageSize(pageSize);
        param.setUserId(queryRequest.getUserId());
        Long count = conceptManager.countByParam(param);
        if (count == null || count == 0) {
            return PagingResponse.build(Lists.newArrayList(), 0L, queryRequest.getCurrentPage(), queryRequest.getPageSize());
        }
        List<ConceptDTO> conceptDTOS = conceptManager.queryByParam(param);
        return PagingResponse.build(conceptDTOS, count, queryRequest.getCurrentPage(), queryRequest.getPageSize());
    }

    @Override
    public Boolean modified(ConceptUpdateRequest conceptUpdateRequest) {
        // 参数校验
        String errorMsg = ValidatorUtils.validateAndReturnSingleErrorMsg(conceptUpdateRequest);
        if (StringUtils.isNotBlank(errorMsg)) {
            throw new BusinessException(ErrorConstants.PARAM_ERROR.getErrorCode(), errorMsg);
        }
        ConceptDTO updateConceptDTO = ConceptConverter.INSTANCE.convertRequest2DTO(conceptUpdateRequest);
        conceptManager.update(updateConceptDTO);
        return Boolean.TRUE;
    }

    @Override
    public Boolean deleteById(ConceptDeleteRequest conceptDeleteRequest) {
        // 参数校验
        String errorMsg = ValidatorUtils.validateAndReturnSingleErrorMsg(conceptDeleteRequest);
        if (StringUtils.isNotBlank(errorMsg)) {
            throw new BusinessException(ErrorConstants.PARAM_ERROR.getErrorCode(), errorMsg);
        }
        if(conceptDeleteRequest.getId() != null) {
            conceptManager.deleteById(conceptDeleteRequest.getId());
        }
        return Boolean.TRUE;
    }

    @Override
    public List<ConceptDTO> listAll(ConceptAllQueryRequest queryRequest) {
        // 参数校验
        String errorMsg = ValidatorUtils.validateAndReturnSingleErrorMsg(queryRequest);
        if (StringUtils.isNotBlank(errorMsg)) {
            throw new BusinessException(ErrorConstants.PARAM_ERROR.getErrorCode(), errorMsg);
        }
        ConceptParam param = new ConceptParam();
        param.setUserId(queryRequest.getUserId());
        List<ConceptDTO> conceptDTOS = conceptManager.queryAllByParam(param);
        return conceptDTOS;
    }
}
